package cn.tedu.boyou.comment.service.impl;

import cn.tedu.boyou.comment.domain.ReviewComment;
import cn.tedu.boyou.comment.domain.SightReview;
import cn.tedu.boyou.comment.repository.IReviewCommentRepository;
import cn.tedu.boyou.comment.repository.ISightReviewRepository;
import cn.tedu.boyou.comment.service.ISightReviewService;
import cn.tedu.boyou.common.query.ReviewCommentQuery;
import cn.tedu.boyou.common.query.SightReviewQuery;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class SightReviewServiceImpl implements ISightReviewService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ISightReviewRepository dao;

    @Autowired
     private IReviewCommentRepository commentRepository;

    public void save(SightReview review) {
        review.setCreateTime(new Date());
        dao.save(review);
    }

    public Page<SightReview> query(SightReviewQuery qo) {
        Query query = new Query();
        Criteria criteria = null;
        if(qo.getSightId() != -1){
            criteria =Criteria.where("sightId").is(qo.getSightId());
            if(qo.getStartype() != -1) {
                System.out.println(qo.getStarnum());
                criteria.andOperator(Criteria.where("totalStarNum").gte(qo.getStarnum().getMinStar()).lte(qo.getStarnum().getMaxStar()));
            }
        }else {
            criteria= Criteria.where("sightId").ne(qo.getSightId());

        }

       query.addCriteria(criteria);

        long total = mongoTemplate.count(query, SightReview.class);
        if(total == 0){
            return Page.empty();
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() -1, qo.getPageSize());
        query.with(pageable);
        Sort createTime = Sort.by(Sort.Direction.DESC, "createTime");
        query.with(createTime);
        List<SightReview> list = mongoTemplate.find(query, SightReview.class);

         for(SightReview sightReview :list){
             //查询出每一个sightReview的回复列表，并且设置进去
            query = new Query();
             ReviewCommentQuery qo1 = new ReviewCommentQuery();
             qo1.setReviewId(sightReview.getId());
             criteria = null;
             //根据点评的id去查询所有的回复返回
             if(!qo1.getReviewId().equals("-1")){
                 criteria = Criteria.where("reviewId").is(qo1.getReviewId());
             }else{
                 criteria = Criteria.where("reviewId").ne(qo1.getReviewId());
             }
             query.addCriteria(criteria);
             List<ReviewComment> listComment= mongoTemplate.find(query, ReviewComment.class);

             sightReview.setRcs(listComment);
         }
        return new PageImpl<SightReview>(list,pageable,total);
    }

    public boolean thumbUp(String toid, Long fromid) {
        /*SightReview comment = mongoTemplate.findById(toid, SightReview.class);*/
        Optional<SightReview> optional = dao.findById(toid);
        SightReview review = optional.get();
        int thumbnum = review.getThumbNum();
        List<Long> thumbuplist = review.getUserIds();
        boolean flag = false;
        if(!thumbuplist.contains(fromid)){
            review.setThumbNum(thumbnum +1);
            thumbuplist.add(fromid);
            flag = true;
        }else{
            review.setThumbNum(thumbnum -1);
            thumbuplist.remove(fromid);
            flag = false;
        }
        dao.save(review);
        return flag;
    }

    public SightReview getSightReviewById(String rid) {
         return dao.findById(rid).get();
    }




    public List<SightReview> selectByUserId(Long uid) {
        Query query = new Query();

        query.addCriteria(Criteria.where("userId").is(uid));


        return mongoTemplate.find(query,SightReview.class);
    }


}
